package livesun.justiceeternal.retorfitP;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Response;

class ErrorCodeInterceptor implements Interceptor {
   //查询参数丢失
   public static final int ERROR_CODE_REQUST = 400;
   //系统错误
   public static final int ERROR_CODE_2 = 500;
   public static final int ERROR_CODE_1 = 503;
   public static final int ERROR_CODE_4 = 401;
   public static final int ERROR_CODE_3 = 403;
   //调用太多
   public static final int ERROR_CODE_MORE= 429;


   @Override
   public Response intercept(Chain chain) throws IOException {
       Response response = chain.proceed(chain.request());  //如果401了，会先执行TokenAuthenticator
       if (response.code() == ERROR_CODE_1||
               response.code() == ERROR_CODE_2||
               response.code() == ERROR_CODE_3||
               response.code() == ERROR_CODE_4
               ){
           throw new ErrorCodeException(response.code(),"System error");
       }else if(response.code() == ERROR_CODE_REQUST){
           throw new ErrorCodeException(response.code(),"Make Sure the valid characters are scanned");
       }else if(response.code() == ERROR_CODE_MORE){
           throw new ErrorCodeException(response.code(),"Server busy,please try it later");
       }
       return response;
   }
}
